Cache the canonical URL for a source
authorAlex Crichton <alex@alexcrichton.com>
Mon, 1 Jun 2015 18:40:49 +0000 (11:40 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 3 Jun 2015 01:05:47 +0000 (18:05 -0700)
This avoids reallocating and recalculating it on each call to
SourceIdInner::{eq, hash}, which are called quite often in the backend.

src/cargo/core/source.rs

index 7551161bc62cb73954d7fc4c13cbbfa2ae5024e4..6583f99a7d2b6b218c52fb994f5e8517ed57de3d 100644 (file)
@@ -72,6 +72,7 @@ pub struct SourceId {
 #[derive(Eq, Clone, Debug)]
 struct SourceIdInner {
     url: Url,
+    canonical_url: Url,
     kind: Kind,
     // e.g. the exact git revision of the specified branch for a Git Source
     precise: Option<String>
@@ -82,6 +83,7 @@ impl SourceId {
         SourceId {
             inner: Arc::new(SourceIdInner {
                 kind: kind,
+                canonical_url: git::canonicalize_url(&url),
                 url: url,
                 precise: None,
             }),
@@ -315,10 +317,9 @@ impl PartialEq for SourceIdInner {
         if self.kind != other.kind { return false }
         if self.url == other.url { return true }
 
-        match (&self.kind, &other.kind, &self.url, &other.url) {
-            (&Kind::Git(ref ref1), &Kind::Git(ref ref2), u1, u2) => {
-                ref1 == ref2 &&
-                git::canonicalize_url(u1) == git::canonicalize_url(u2)
+        match (&self.kind, &other.kind) {
+            (&Kind::Git(ref ref1), &Kind::Git(ref ref2)) => {
+                ref1 == ref2 && self.canonical_url == other.canonical_url
             }
             _ => false,
         }
@@ -329,8 +330,8 @@ impl hash::Hash for SourceId {
     fn hash<S: hash::Hasher>(&self, into: &mut S) {
         self.inner.kind.hash(into);
         match *self.inner {
-            SourceIdInner { kind: Kind::Git(..), ref url, .. } => {
-                git::canonicalize_url(url).hash(into)
+            SourceIdInner { kind: Kind::Git(..), ref canonical_url, .. } => {
+                canonical_url.hash(into)
             }
             _ => self.inner.url.hash(into),
         }